package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

/**
 * AbstractAnnotationConfigDispatcherServletInitializer会创建DispatcherServlet及ContextLoadderListener
 */
public class SpittrWebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer{
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};//设置Spring应用容器，业务组件配置(数据源等)
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};//设置SpringMVC容器，web组件配置(控制器、解析器等)
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }


    /**
     * 可以在此方法中设置文件上传相关配置
     * 及load-on-startup优先级
     * @param registration
     */
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        String tempPath = "/tmp/spittr/upload";
        if(System.getProperty("os.name").toUpperCase().contains("WINDOWS")){
            tempPath = "D:/spitter/upload";
        }
        //设置上传文件的临时目录，以及单个文件的最大为2M，请求体最大为4M,文件全部写入硬盘中
        registration.setMultipartConfig(new MultipartConfigElement(tempPath,2*1024*1024,4*1024*1024,0));//设置文件上传目录
    }


    /**
     * 添加过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        return  super.getServletFilters();
    }




}
